package com.basic.system.service.impl;

import com.basic.common.constant.CommonDsConstant;
import com.basic.common.exception.Errors;
import com.basic.common.exception.business.ServerServiceException;
import com.basic.system.dao.mapper.SystemFileExMapper;
import com.basic.system.dao.mapper.SystemFileMapper;
import com.basic.system.domain.SystemFile;
import com.basic.system.domain.SystemFileExample;
import com.basic.system.domain.bo.SystemFileBo;
import com.basic.system.service.SystemService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class SystemServiceImpl implements SystemService {

    @Autowired
    private SystemFileMapper systemFileMapper;
    @Resource
    private SystemFileExMapper systemFileExMapper;

    //添加新文件
    @Override
    @Transactional
    public int insert(SystemFile systemFile) {
        systemFile.setType(getFileType(systemFile.getUrl()));
        return systemFileMapper.insertSelective(systemFile);
    }

    //批量添加新文件
    @Override
    @Transactional
    public int insertById(List<SystemFile> systemFiles) {
        int i = 0;
        for (SystemFile file : systemFiles) {
            file.setCreateTime(new Date());
            systemFileMapper.insertSelective(file);
            i += 1;
        }
        return i;
    }

    //根据业务ID字符串批量添加文件
    @Override
    @Transactional
    public int insertByRefStrId(List<SystemFile> systemFiles, String refStrId, String BusType) {
        int i = 0;
        for (SystemFile file : systemFiles) {
            file.setCreateTime(new Date());
            file.setRefStrId(refStrId);
            file.setBusType(BusType);
            file.setType(getFileType(file.getUrl()));
            systemFileMapper.insertSelective(file);
            i += 1;
        }
        return i;
    }

    //根据业务ID批量添加文件
    @Override
    @Transactional
    public int insertByRefId(List<SystemFile> systemFiles, Long refId) {
        int i = 0;
        for (SystemFile file : systemFiles) {
            file.setCreateTime(new Date());
            file.setRefId(refId);
            //设置文件类型
            file.setType(getFileType(file.getUrl()));
            systemFileMapper.insertSelective(file);
            i += 1;
        }
        return i;
    }


    //根据文件ID逻辑删除，使文件变不可用
    @Override
    @Transactional
    public int delByFileId(Long id) {
        if (systemFileExMapper.delById(id) > 0){
            return 1;
        }
        throw new ServerServiceException(Errors.SYSTEM_DELETE_ERROR);
    }

    //根据业务ID逻辑删除，使文件变不可用
    @Override
    @Transactional
    public int deleteByRefId(SystemFile systemFile) {
        if (systemFile != null && systemFile.getRefId() != null) {
            SystemFileExample fileExample = new SystemFileExample();
            SystemFileExample.Criteria criteria = fileExample.createCriteria();
            criteria.andRefIdEqualTo(systemFile.getRefId());
            systemFile.setIsEnabled(CommonDsConstant.IS_NOT_ENABLED);
            return systemFileMapper.updateByExampleSelective(systemFile, fileExample);
        }
        return -1;
    }

    @Override
    @Transactional
    public int deleteByRefStrId(SystemFile systemFile) {
        if (systemFile != null && systemFile.getRefStrId() != null) {
            SystemFileExample fileExample = new SystemFileExample();
            SystemFileExample.Criteria criteria = fileExample.createCriteria();
            criteria.andRefStrIdEqualTo(systemFile.getRefStrId());
            systemFile.setIsEnabled(CommonDsConstant.IS_NOT_ENABLED);
            return systemFileMapper.updateByExampleSelective(systemFile, fileExample);
        }
        return -1;
    }


    //批量删除文件，逻辑删除，使文件变为不可用
    @Override
    @Transactional
    public int deleteByBo(SystemFileBo systemFileBo) {
        if (systemFileBo != null) {
            SystemFileExample fileExample = new SystemFileExample();
            SystemFileExample.Criteria criteria = fileExample.createCriteria();
            if (systemFileBo.getIds() != null) {
                //将获取的文件ID放进集合
                criteria.andIdIn(systemFileBo.getIds());
            }
            if (systemFileBo.getRefStrIds() != null) {
                //将获取的文件业务字符串ID放进集合
                criteria.andRefStrIdIn(systemFileBo.getRefStrIds());
            }
            if (systemFileBo.getRefIds() != null) {
                //将获取的文件业务ID放进集合
                criteria.andRefIdIn(systemFileBo.getRefIds());
            }
            //将集合里文件都变为不可用文件
            systemFileBo.setIsEnabled(CommonDsConstant.IS_NOT_ENABLED);
            return systemFileMapper.updateByExampleSelective(systemFileBo, fileExample);
        }
        return -1;
    }


    //更新文件信息
    @Override
    @Transactional
    public int updateFile(SystemFile systemFile) {
        return systemFileMapper.updateByPrimaryKeySelective(systemFile);
    }


    //不分页查询文件
    @Override
    public List<SystemFile> selectByBo(SystemFile systemFile) {
//        Integer pageNum = CommonDsConstant.PAGE_NUM;
//        Integer pageSize = CommonDsConstant.PAGE_SIZE;
//        if (systemFileBo.getPageNum() != null && systemFileBo.getPageSize() != null) {
//            pageNum = systemFileBo.getPageNum();
//            pageSize = systemFileBo.getPageSize();
//        }
//        //设置分页
//        PageHelper.startPage(pageNum, pageSize);
        SystemFileExample fileExample = new SystemFileExample();
        SystemFileExample.Criteria criteria = fileExample.createCriteria();
        if (systemFile != null) {
            //根据文件ID查询可用文件
            if (systemFile.getId() != null) {
                criteria.andIdEqualTo(systemFile.getId());
            }
            //根据用户ID模糊查询可用文件
            if (systemFile.getBoId() != null) {
                criteria.andBoIdLike(systemFile.getBoId());
            }
            //根据业务ID查询可用文件
            if (systemFile.getRefId() != null) {
                criteria.andRefIdEqualTo(systemFile.getRefId());
            }
            //根据业务ID字符串模糊查询可用文件
            if (systemFile.getRefStrId() != null) {
                criteria.andRefStrIdLike("%" + systemFile.getRefStrId() + "%");
            }
            //根据文件类型模糊查询可用文件
            if (systemFile.getType() != null) {
                criteria.andTypeLike("%" + systemFile.getType() + "%");
            }
            //根据业务类型模糊查询可用文件
            if (systemFile.getBusType() != null) {
                criteria.andBusTypeLike("%" + systemFile.getBusType() + "%");
            }
            //根据系统ID查询可用文件
            if (systemFile.getSystemId() != null) {
                criteria.andSystemIdEqualTo(systemFile.getSystemId());
            }
            //查询所有可用文件
            criteria.andIsEnabledEqualTo(CommonDsConstant.IS_ENABLED);
        }
        return systemFileMapper.selectByExample(fileExample);
    }


    //分页查询
    @Override
    public PageInfo<SystemFile> pageList(SystemFileBo bo) {
        Integer pageNum = CommonDsConstant.PAGE_NUM;
        if (bo.getPageNum() != null) {
            pageNum = bo.getPageNum();
        }
        Integer pageSize = CommonDsConstant.PAGE_SIZE;
        if (bo.getPageSize() != null) {
            pageSize = bo.getPageSize();
        }
        //设置分页
        PageHelper.startPage(pageNum, pageSize);
        return new PageInfo(this.selectByBo(bo));
    }

    /**
     * 从URl取文件类型
     *
     * @param fileName
     * @return
     */
    @Override
    public String getFileType(String fileName) {
        int lastDotIndex = fileName.lastIndexOf('.');
        if (lastDotIndex != -1) {
            return fileName.substring(lastDotIndex + 1);
        } else {
            return "";
        }
    }

}
